package com.truyentranh.requester;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.truyentranh.data.AdsDataObject;
import com.truyentranh.data.HaiBLMetalData;
import com.truyentranh.data.HaiBLModule;
import com.truyentranh.data.NewsDataObject;
import com.truyentranh.data.RssDataObject;
import com.truyentranh.data.RssEntryDataObject;
import com.truyentranh.data.RssEntryDataObject.Entry;
import com.truyentranh.network.Request;
import com.truyentranh.network.RequestListener;
import com.truyentranh.network.UrlHelper;
import com.truyentranh.utils.AppSettingParams;
import com.truyentranh.utils.Logger;

public class RssRequester extends Request {

	static private final int RSS_TITLE = 1;
	static private final int RSS_CONTENT = 2;
	
	private RequestListener requestListener;
	private int newId = -1;
	private int method = RSS_CONTENT;

	public RssRequester(int newId, RequestListener listener) {
		this.requestListener = listener;
		this.newId = newId;
	}
	
	public static RssRequester getRssTitleByNews(int newsId, RequestListener listener){		
		RssRequester requester = new RssRequester(newsId, listener);
		requester.method = RSS_TITLE;
		return requester;
	}

	@Override
	public void cancelRequest() {
		// TODO Auto-generated method stub
	}

	@Override
	public void runRequest() {
		String base = "";
		if(method == RSS_TITLE){
			base = UrlHelper.BASE_URL_RSS_TITLE_ALL;
		} else if ( method == RSS_CONTENT){
			base = UrlHelper.BASE_URL_RSS_ALL;
		}
		// TODO Auto-generated method stub
		url = UrlHelper.getUrlOfMethod(base, 
										new String[] { "id" }, 
										new String[] { String.valueOf(newId) });
		send();
	}

	private ArrayList<RssEntryDataObject> arrayList = new ArrayList<RssEntryDataObject>();
	private boolean myself = false;

	public boolean isMySelf() {
		return this.myself;
	}

	@Override
	public void requestFinished(JSONObject jsonObject) {

		if (jsonObject != null) {
			try {
				if (jsonObject.has("data")) {
					JSONArray array = jsonObject.getJSONArray("data");
					if (array != null) {
						for (int i = 0, n = array.length(); i < n; i++) {
							JSONObject childAds = array.getJSONObject(i);

							int id = childAds.has("id") ? childAds.getInt("id") : -1;
							String name = childAds.has("name") ? childAds.getString("name") : "";
							String link = childAds.has("link") ? childAds.getString("link") : "";

							RssEntryDataObject adsDataObject = new RssEntryDataObject(id, name, link);
							JSONArray entries = childAds.has("entries") ? childAds.getJSONArray("entries") : null;
							if (entries != null) {
								for (int j = 0, s = entries.length(); j < s; j++) {
									JSONObject childentries = entries.getJSONObject(j);

									String title = childentries.has("title") ? childentries.getString("title") : "";
									String img = childentries.has("img") ? childentries.getString("img") : "";
									String linkDetail = childentries.has("link") ? childentries.getString("link") : "";
									String pubdate = childentries.has("pubdate") ? childentries.getString("pubdate") : "";

									adsDataObject.addEntry(new Entry(title, img, linkDetail, pubdate));
								}
							}
							arrayList.add(adsDataObject);
						}
					}
				}
				myself = jsonObject.has("self") ? (jsonObject.getInt("self") == 1 ? true : false) : false;

				if (requestListener != null) {
					requestListener.onRequestComplete(this);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void requestFailed(int errorCode, String message) {
		// TODO Auto-generated method stub
		if (requestListener != null) {
			requestListener.onRequestFailed(message, this);
		}
	}

	/**
	 * @return the arrayList
	 */
	public ArrayList<RssEntryDataObject> getArrayList() {
		return arrayList;
	}
}
